class xG_Missions_Manager extends Actor notplaceable;

//=============================================================================
// Variables
//=============================================================================
var private xG_GFx_MissionWindow		            mMissionWindow;
var private x_GameInfo                              mGameInfo;
var private xG_PlayerController                     mPlayerController;
var private xG_Missions_Info                        mMission_Info;

//=============================================================================
// Functions:
//=============================================================================
simulated function PostBeginPlay()
{
	super.PostBeginPlay();
	mGameInfo = x_GameInfo(WorldInfo.Game);
	mPlayerController = xG_PlayerController(mGameInfo.GetPlayerController());
	Create_MissionWindow();
	mMission_Info = new(self)class'xG_Missions_Info';
	mMission_Info.Initalize(mGameInfo);
}

function Create_MissionWindow()
{
	mMissionWindow = new class'xG_GFx_MissionWindow';
	mMissionWindow.InitUI(mGameInfo);
	mMissionWindow.SetViewScaleMode( SM_NoScale );
	mMissionWindow.SetAlignment( Align_TopLeft );
	mMissionWindow.ClearEventsList();
}

function string GetMissionMapName()
{
	return mMission_Info.MissionInfo.MapName;
}

//=============================================================================
// States:
//=============================================================================
AUTO state Missions
{
	function Get_Mission_Data()
	{
		local int lMissionIndex;
		if (mMission_Info.GetNextMissionIndex(lMissionIndex))
		{
			mMission_Info.GetMissionInfo(lMissionIndex);
		}
	}

Begin:
	//whait while scaleform animation compleated and then go to mission execution
	//sleep(1.f);

Mission:
	Get_Mission_Data();
	Load_Mission();
}

//=============================================================================
// Missions
//=============================================================================
function Load_Mission()
{
	mPlayerController.UpdateEarthView(mMission_Info.MissionInfo.Player_Rotation); // turn to mission location in earth
	AddLight(mPlayerController);
	//mMissionWindow.SetScreenPosition(100, 400); //
	mMissionWindow.AddNewEvent(mMission_Info.MissionInfo.MissionText);
	PlaceMissionMarker(mMission_Info.MissionInfo.MarkerLocation);
}

//=============================================================================
// Missions helpers
//=============================================================================
function PlaceMissionMarker(vector aOnEarthLocation)
{
	local x_Tile lMarker;
	local Rotator lMarkerRotation;
	local Vector lMarkerLocation;
	local Vector lWorldCenter;

	//correct marker location
	lWorldCenter = class'x_Settings'.default.GEO_WorldCenter;
	lMarkerLocation = lWorldCenter - ((Normal(lWorldCenter - aOnEarthLocation)) * class'x_Settings'.default.GEO_EarthRadius); 

	//marker rotation always perpendicular earth surface
	lMarkerRotation = Rotator(lWorldCenter - aOnEarthLocation);
	lMarkerRotation.Pitch += 90.0f * DegToRad * RadToUnrRot; 

	//place marker
	lMarker = spawn(class'x_Tile',self,'Marker',lMarkerLocation,lMarkerRotation,,false);
	lMarker.SetStaticMesh(StaticMesh(DynamicLoadObject(class'x_Settings'.default.MarkerMesh, class'StaticMesh')));
}

function AddLight(x_playercontroller aPC)
{
	local DominantDirectionalLightComponent lSunLights;
	lSunLights = new(self)class'DominantDirectionalLightComponent';
	//lSunLights.SetLightProperties(SunParams.LightBrightness, SunParams.LightColor);
	//lSunLights.CastDynamicShadows = true;
    //lSunLights.MaxShadowResolution = 100;
   // lSunLights.MinShadowResolution = 1;
	//lSunLights.ShadowFilterQuality = SFQ_High;
	//lSunLights.ShadowProjectionTechnique = ShadowProjTech_BPCF_High;
	SetLocation(aPC.Location);
	SetRotation(aPC.Rotation);
	if ( lSunLights != None ) aPC.AttachComponent(lSunLights);
}

//=============================================================================
// Default Properties
//=============================================================================
DefaultProperties
{

}